package com.tarena.lbs.coupon.dao.repository.impl;

import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class CouponRepositoryImpl implements CouponRepository {
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public void save(CouponPO couponPO) {
        log.info("保存优惠券业务:{}"+couponPO);
        /*CouponPO couponPO = new CouponPO();
        BeanUtils.copyProperties(couponParam, couponPO);
        couponPO.setCreateAt(new Date());
        couponPO.setUpdateAt(new Date());
        // 确保必填字段有值
        if (couponPO.getStartDate() == null) {
            couponPO.setStartDate(new Date()); // 设置默认开始时间为当前时间
        }
        // 如果没有设置结束时间，默认设置为开始时间后30天
        if (couponPO.getEndDate() == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(couponPO.getStartDate());
            calendar.add(Calendar.DAY_OF_MONTH, 30); // 默认30天有效期
            couponPO.setEndDate(calendar.getTime());
        }
        couponPO.setStatus(1);
        couponPO.setEnableStatus(1);*/
        couponMapper.insert(couponPO);
    }

    @Override
    public CouponPO detail(Integer id) {
        return couponMapper.selectById(id);
    }

    @Override
    public List<CouponVO> pageList(CouponQuery couponQuery) {
        List<CouponVO> pageResult = couponMapper.pageList(couponQuery);
        return pageResult;
    }

    @Override
    public void delete(Integer id) {
        couponMapper.deleteById( id);
    }
}
